<?php
/**
 * one主题主要功能文件
 *
 * @package one
 * @subpackage Functions
 */

/**
 * 为主题添加恰当的文档信息
 * 默认为 XHTML 1.0 Strict
 *
 * @since 1.0.0
 */
function one_doctype() {
	$browser = $_SERVER[ 'HTTP_USER_AGENT' ];
	if ( !preg_match( "/MSIE 6.0/", $browser ) )
		echo '<?xml version="1.0" encoding="' . get_bloginfo( 'charset' ) . '"?>' . "\n";

	$doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' . "\n";
	echo apply_filters( 'one_doctype', $doctype );
}

/**
 * 试图在任何情况下为搜索引擎作出最好的优化（SEO）
 *
 * 对All-in-One SEO和HeadSpace2提供良好兼容性
 * @链接 http://wordpress.org/extend/plugins/all-in-one-seo-pack
 * @链接 http://wordpress.org/extend/plugins/headspace2
 *
 * @since 1.0.0
 * @return string
 */
function one_document_title() {
	global $post, $wp_query;
	
	/*
	* 为插件提供兼容
	* All-in-One SEO Pack
	* HeadSpace2
	*/
	if ( class_exists( 'All_in_One_SEO_Pack' ) || class_exists( 'HeadSpace2_Admin' ) ) :
		wp_title( '' );
		return;
	endif;

	$doctitle = array();

	/*
	* 开始处理自定义标题
	*/
	if ( is_front_page() || is_home() ) :
		$content = '';
	elseif ( is_attachment() ) :
		$content = single_post_title( false, false );
	elseif ( is_single() || is_page() ) :
		$content = wp_specialchars( get_post_meta( $post->ID, 'Title', true ), true );
		if ( !$content ) :
			$content = single_post_title( false, false );
		else :
			$subtitle = wp_specialchars( get_post_meta( $post->ID, 'Subtitle', true ), true );
			if ( $subtitle ) $content .= ': ' . $subtitle;
		endif;
	elseif ( is_category() ) :
		$content = single_cat_title( false, false );
	elseif ( is_tag() ) :
		$content = single_tag_title( false, false );
	elseif ( is_author() ) :
		$content = wp_title( false, false, false );
	elseif ( is_search() ) :
		$content = sprintf( __('关键字&quot;%1$s&quot;的搜索结果', 'one'), attribute_escape( get_search_query() ) );
	elseif ( is_day() ) :
		$content = sprintf( __('%1$s的存档', 'one'), get_the_time( __('F jS, Y', 'one') ) );
	elseif ( get_query_var( 'w' ) ) :
		$content = sprintf( __('%2$s年，第%1$s周的存档', 'one'), get_the_time( __('W', 'one') ), get_the_time( __('Y', 'one') ) );
	elseif ( is_month() ) :
		$content = sprintf( __('%1$s的存档', 'one'), single_month_title( ' ', false) );
	elseif ( is_year() ) :
		$content = sprintf( __('%1$s的存档', 'one'), get_the_time( __('Y', 'one') ) );
	elseif ( is_comments_popup() ) :
		$content = sprintf( __('Comment on &quot;%1$s&quot;', 'one'), single_post_title( false, false ) );
	elseif ( is_404() ) :
		$content = __('未找到（404）', 'one');
	else :
		$content = wp_title( false, false, false );
	endif;

	/*
	* 设置$doctitle数组
	*/
	$doctitle['sitename'] = get_bloginfo( 'name' );
	$doctitle['tagline'] = get_bloginfo( 'description' );
	$doctitle['separator'] = ':';
	$doctitle['content'] = $content;

	/*
	* Not quite ready to apply filters (maybe 0.5.1)
	*/
	//$doctitle = apply_filters( 'one_document_title', $doctitle );

	/*
	* 如果分页
	*/
	if ( ( ( $page = $wp_query->get( 'paged' ) ) || ( $page = $wp_query->get( 'page' ) ) ) && $page > 1 )
		$doctitle['content'] .= sprintf( __('%1$s Page %2$s', 'one'), $doctitle['separator'], $page );

	extract( $doctitle );

	/*
	* 输出方式
	*/
	if ( is_home() || is_front_page() )
		$title = $tagline . $separator . '-' . $sitename . $content;
	else
		$title = $content . $separator . '-' . $sitename;

	echo $title;
}
/**
 * 在头部设置文档类型
 *
 * @since 1.0.0
*/
function one_meta_content_type() {
	$content_type = '<meta http-equiv="Content-Type" content="' . get_bloginfo( 'html_type' ) . ' charset=' . get_bloginfo( 'charset' ) . '" />' . "\n";
	echo apply_filters( 'one_meta_content_type', $content_type );
}

/**
 * 允许搜索引擎的索引
 *
 * @since 1.0.0
 */
function one_meta_robots() {
	global $post, $wp_query;

	if ( !get_option( 'blog_public' ))
		return;

	$follow = true;
	
	if ( ( is_home() || is_front_page() ))
		$follow = true;
	elseif ( is_attachment() )
		$follow = true;
	elseif ( is_single() && !is_attachment() )
		$follow = true;
	elseif ( is_page() && !is_front_page() )
		$follow = true;
	elseif (is_author() && is_tag() && is_date() && is_category() )
		$follow = false;
	elseif ( is_search() && $hybrid_settings['robots_search'] )
		$follow = true;
	elseif ( is_404() && $hybrid_settings['robots_404'] )
		$follow = true;
	else
		$follow = false;

	if ( is_singular() && get_option( 'page_comments' )) :
		if ( isset( $wp_query->query['cpage'] ) && absint( $wp_query->query['cpage'] ) >= 1 ) :
			$follow = false;
		endif;
	endif;

	if ( $follow )
		$robots = '<meta name="robots" content="index,follow" />' . "\n";
	else
		$robots = '<meta name="robots" content="noindex,follow" />' . "\n";

	echo apply_filters( 'one_meta_robots', $robots );
}

/**
 * 生成meta作者信息
 *
 * @since 1.0.0
 */
function one_meta_author() {
	global $wp_query, $post;

	if (is_single() || is_page()) :
		$curauth = get_userdata( $wp_query->post->post_author );
		if ( $curauth->first_name && $curauth->last_name ) :
			$meta_author = $curauth->first_name . ' ' . $curauth->last_name;
		elseif ( $curauth->display_name ) :
			$meta_author = $curauth->display_name;
		elseif ( $curauth->nickname ) :
			$meta_author = $curauth->nickname;
		elseif ( $curauth->user_nicename ) :
			$meta_author = $curauth->user_nicename;
		else :
			$meta_author = $curauth->user_login;
		endif;
	endif;

	if ( $meta_author )
		echo '<meta name="author" content="' . $meta_author . '" />' . "\n";
}

/**
 * @since 1.0.0
 */
function one_meta_copyright() {
	if ( is_single() || is_page() )
		$date = get_the_time( __('F Y', 'one') );
	else
		$date = date( __('Y', 'one') );

	$copyright = sprintf( __('Copyright (c) %1$s', 'one'), $date );
	echo '<meta name="copyright" content="' . $copyright . '" />' . "\n";
}

/**
 * @since 1.0.0
 */
function one_meta_revised() {
	if ( is_single() || is_page() ) :
		$date = get_the_modified_time( __('Y-m-d, l, H:i', 'one') );
		echo '<meta name="revised" content="' . $date . '" />' . "\n";
	endif;
}

/**
 *
 * @since 1.0.0
 */
function one_meta_description() {
	global $post, $wp_query;

	if ( class_exists( 'All_in_One_SEO_Pack' ) || class_exists( 'HeadSpace2_Admin' ) )
		return;

	if ( is_home() || is_front_page() ) :
		$meta_desc = get_bloginfo( 'description' );

	elseif ( is_single() || is_page() ) :
		$meta_desc = get_post_meta( $post->ID, 'Description', true );

		if ( !$meta_desc && $one_settings['seo_single_excerpts'] ) :
			$meta_desc = get_the_excerpt();
		endif;

	elseif ( is_category() ) :
		$meta_desc = stripslashes( strip_tags( category_description() ) );

	elseif ( is_author() ) :
		$meta_auth = get_userdata( get_query_var( 'author' ) );
		$meta_desc = str_replace( array( '"' ), '&quot;', $meta_auth->description );

	endif;

	if ( !$meta_desc || strlen( $meta_desc ) <= 1 )
		return;

	$meta_desc = str_replace( array( "\r", "\n", "\t" ), '', wp_specialchars( $meta_desc, 1 ) );
	$meta_desc = '<meta name="description" content="' . $meta_desc . '" />' . "\n";
	echo apply_filters( 'one_meta_description', $meta_desc );
}

/**
 *
 * @since 1.0.0
 */
function one_meta_keywords() {
	global $post, $wp_query;

	if ( class_exists( 'All_in_One_SEO_Pack' ) || class_exists( 'HeadSpace2_Admin' ) )
		return;

	if ( is_single() && !is_preview() && !is_attachment() ) :
		$post_keywords = get_post_meta( $post->ID, 'Keywords', true );
		if ( $post_keywords )
			$keywords[] = $post_keywords;

		 if( !$post_keywords ) :
			$cats = get_the_category();
			if ( $cats ) :
				foreach( $cats as $cat ) :
					$keywords[] = $cat->name;
				endforeach;
			endif;
		endif;
		if (!$post_keywords ) :
			$wp_query->in_the_loop = true;
			$tags = get_the_tags();
			if ( $tags ) :
				foreach ( $tags as $tag ) :
					$keywords[] = $tag->name;
				endforeach;
			endif;
			$wp_query->in_the_loop = false;
		endif;

	elseif(is_page() && !is_preview()) :
		$post_keywords = get_post_meta( $post->ID, 'Keywords', true );
		if ( $post_keywords )
			$keywords[] = $post_keywords;
	endif;

	if ( !empty( $keywords ) ) :
		$keywords = join( ', ', $keywords );
		$keywords = '<meta name="keywords" content="' . wp_specialchars( $keywords, 1 ) . '" />' . "\n";
	endif;

	echo apply_filters( 'one_meta_keywords', $keywords );
}

/**
 *
 * @since 1.0.0
 */
function one_meta_template() {
	$data = get_theme_data( TEMPLATEPATH . '/style.css' );
	echo '<meta name="template" content="' . $data['Title'] . ' ' . $data['Version'] . '" />' . "\n";
}

/**
 *
 * @since 1.0.0
 */
function one_favicon() {
	$favicon = get_stylesheet_directory_uri() . '/images/favicon.ico';
	$favicon =  '<link rel="shortcut icon" type="image/x-icon" href="' . $favicon . '" />' . "\n";
	echo apply_filters( 'one_favicon', $favicon );
}

/**
 *
 * @since 1.0.0
 */
function one_head_feeds() {
	global $wp_query, $post, $one_settings;

	if ( function_exists( 'automatic_feed_links' ) )
		automatic_feed_links( false );

	if ( $one_settings['feeds_redirect'] ) :
		$rdf = $one_settings['feed_url'];
		$rss2 = $one_settings['feed_url'];
		$rss = $one_settings['feed_url'];
		$atom = $one_settings['feed_url'];

	elseif ( is_category() ) :
		$cat = $wp_query->get_queried_object();
		$rdf = get_category_feed_link( $cat->term_id, 'rdf' );
		$rss2 = get_category_feed_link( $cat->term_id, 'rss2' );
		$rss = get_category_feed_link( $cat->term_id, 'rss' );
		$atom = get_category_feed_link( $cat->term_id, 'atom' );

	elseif ( is_tag() ) :
		$tag = $wp_query->get_queried_object();
		$rdf = get_tag_feed_link( $tag->term_id, 'rdf' );
		$rss2 = get_tag_feed_link( $tag->term_id, 'rss2' );
		$rss = get_tag_feed_link( $tag->term_id, 'rss' );
		$atom = get_tag_feed_link( $tag->term_id, 'atom' );

	elseif ( is_author() ) :
		$author = $wp_query->get_queried_object();
		$rdf = get_author_feed_link( $author->ID, 'rdf' );
		$rss2 = get_author_feed_link( $author->ID, 'rss2' );
		$rss = get_author_feed_link( $author->ID, 'rss' );
		$atom = get_author_feed_link( $author->ID, 'atom' );

	elseif ( is_search() ) :
		$search = attribute_escape( get_search_query() );
		$rdf = get_search_feed_link( $search, 'rdf' );
		$rss2 = get_search_feed_link( $search, 'rss2' );
		$rss = get_search_feed_link( $search, 'rss' );
		$atom = get_search_feed_link( $search, 'atom' );

	else :
		$rdf = get_bloginfo( 'rdf_url' );
		$rss2 = get_bloginfo( 'rss2_url' );
		$rss = get_bloginfo( 'rss_url' );
		$atom = get_bloginfo( 'atom_url' );
	endif;


	if ( ( is_single() && comments_open() ) || ( is_page() && comments_open() ) )
		$comments_rss2 = get_post_comments_feed_link( $post->ID );
	else
		$comments_rss2 = get_bloginfo( 'comments_rss2_url' );


	$feeds = false;
	if ( $rss2 )
		$feeds .= '<link rel="alternate" type="application/rss+xml" title="' . __('RSS 2.0', 'one') . '" href="' . $rss2 . '" />' . "\n";
	if ( $rdf )
		$feeds .= '<link rel="alternate" type="application/rss+xml" title="' . __('RDF/RSS 1.0', 'one') . '" href="' . $rdf . '" />' . "\n";
	if ( $rss )
		$feeds .= '<link rel="alternate" type="text/xml" title="' . __('RSS .92', 'one') . '" href="' . $rss . '" />' . "\n";
	if ( $atom )
		$feeds .= '<link rel="alternate" type="application/atom+xml" title="' . __('Atom 0.3', 'one') . '" href="' . $atom . '" />' . "\n";
	if ( $comments_rss2 )
		$feeds .= '<link rel="alternate" type="application/rss+xml" title="' . __('Comments RSS 2.0', 'one') . '" href="' . $comments_rss2 . '" />' . "\n";

	echo apply_filters( 'one_head_feeds', $feeds );
}

/**
 *
 * @since 1.0.0
 */
function one_head_pingback() {
	echo '<link rel="pingback" href="' . get_bloginfo( 'pingback_url' ) . '" />' . "\n";
}

/**
 *
 * @since 1.0.0
 */
function one_head_canonical() {
	global $wp_query, $post;

	if ( is_paged() ) :
		$canonical = '<link rel="canonical" href="' . clean_url( get_pagenum_link( $page ) ) . '" />' . "\n";
		echo apply_filters( 'one_head_canonical', $canonical );
		return;
	endif;

	/*
	* Canonical URLs
	* Not handling paginated posts/pages yet
	*/
	if ( ( ( $page = $wp_query->get( 'paged' ) ) || ( $page = $wp_query->get( 'page' ) ) ) && $page > 1 )
		return;

	if ( is_attachment() ) :
		$canonical = '<link rel="canonical" href="' . get_attachment_link( $wp_query->post->ID ) . '" />' . "\n";
	elseif ( is_page() ) :
		$canonical = '<link rel="canonical" href="' . get_page_link( $wp_query->post->ID ) . '" />' . "\n";
	elseif ( is_single() ) :
		$canonical = '<link rel="canonical" href="' . get_permalink() . '" />' . "\n";
	elseif ( is_front_page() ) :
		$canonical = '<link rel="canonical" href="' . get_bloginfo( 'url' ) . '" />' . "\n";
	elseif ( is_day() ) :
		$canonical = '<link rel="canonical" href="' . get_day_link( get_the_time( 'Y' ), get_the_time( 'm' ), get_the_time( 'd' ) ) . '" />' . "\n";
	elseif ( is_month() ) :
		$canonical = '<link rel="canonical" href="' . get_month_link( get_the_time( 'Y' ), get_the_time( 'm' ) ) . '" />' . "\n";
	elseif ( is_year() ) :
		$canonical = '<link rel="canonical" href="' . get_year_link( get_the_time( 'Y' ) ) . '" />' . "\n";
	elseif ( is_category() ) :
		$cat = $wp_query->get_queried_object();
		$canonical = '<link rel="canonical" href="' .  get_category_link( $cat->term_id ) . '" />' . "\n";
	elseif ( is_tag() ) :
		$tag = $wp_query->get_queried_object();
		$canonical = '<link rel="canonical" href="' . get_tag_link( $tag->term_id ) . '" />' . "\n";
	elseif ( is_author() ) :
		$author = $wp_query->get_queried_object();
		$canonical = '<link rel="canonical" href="' . get_author_posts_url( $author->ID, $author->user_nicename ) . '" />' . "\n";
	endif;

	echo apply_filters( 'one_head_canonical', $canonical );
}

/**
 *
 * @since 1.0.0
 */
function one_head_breadcrumb() {
	global $post;

	$prev = get_previous_post();
	$next = get_next_post();

	/*
	* Breadcrumbs
	*/
	if ( !is_front_page() )
		echo '<link rel="home" title="' . get_bloginfo( 'name' ) . '" href="' . get_bloginfo( 'url' ) . '" />' . "\n";
	if ( is_single() || is_page() )
		echo '<link rel="bookmark" title="' . the_title_attribute( 'echo=0' ) . '" href="' . get_permalink() . '" />' . "\n";
	if ( $prev )
		echo '<link rel="previous" title="' . wp_specialchars( $prev->post_title, 1 ) . '" href="' . get_permalink( $prev->ID ) . '" />' . "\n";
	if ( $next )
		echo '<link rel="next" title="' . wp_specialchars( $next->post_title, 1 ) . '" href="' . get_permalink( $next->ID ) . '" />' . "\n";

	echo "\n";
}

/**
 * 存档和日志的导航链接
 * @链接 http://wordpress.org/extend/plugins/wp-pagenavi
 *
 * @since 1.0.0
 */
function one_navigation_links() {

	if ( is_home() || is_archive() || is_search() || is_page_template( 'blog.php' ) || is_page_template( 'excerpts.php' ) ) :

		if ( function_exists( 'wp_pagenavi' ) ) :
			wp_pagenavi();
		else : ?>
			<div class="navigation-links">
				<?php posts_nav_link(
					'',
					'<span class="previous">&laquo; ' . __('Previous Page', 'one') . '</span>',
					'<span class="next">' . __('Next Page', 'one') . ' &raquo;</span>'
				); ?>
			</div>

		<?php endif;

	elseif ( is_single() || is_attachment() ) : ?>

		<div class="navigation-links">
			<?php previous_post_link( '<span class="previous">&laquo; %link</span>' ); ?> 
			<?php next_post_link( '<span class="next">%link &raquo;</span>' ); ?>
		</div>
	<?php
	endif;
}

/**
 * Footer insert from the theme settings page
 * Uses stripslashes() for proper output of code
 *
 * @since 1.0.0
 */
function one_footer_insert() {
	global $one_settings;
	if ( $one_settings['footer_insert'] )
		echo stripslashes( $one_settings['footer_insert'] );
}


/**
 * Error output message
 * Shouldn't be used much, but just in case, it's here
 *
 * @since 1.0.0
 * @return string
 */
function one_error() {
	_e('You have encountered an error. This is usually because you\'ve changed something in the core one theme files. Try undoing your last edit to correct this issue. If this doesn\'t resolve it, head over to the support forums for help.', 'one');
}

/**
 * 显示数据库查询和加载时间
 *
 * @since 1.0.0
 */
function one_query_counter() {
		echo '<p class="query-count">';
		printf( __('这个页面耗用了%1$s″生成才生成完毕，期间查询了%2$s次数据库.', 'one'), timer_stop( 0, 3 ), get_num_queries() );
		echo '</p>';
}

?>